package com.lfz.search.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class EsConfig implements FactoryBean<TransportClient>, InitializingBean {

    @Autowired
    private ElasticsearchProperties elasticsearchProperties;

    private TransportClient client;

    /**
     * 获取设置对象
     *
     * @return Settings对象
     */
    public Settings getSettings() {
        String clusterName = elasticsearchProperties.getClusterName();
        return Settings.builder().put("cluster.name", clusterName).build();
    }

    /**
     * 预处理所有节点信息
     *
     * @return 包含节点Map的List对象
     */
    public ArrayList<Map<String, Object>> getNodes() {
        String nodes = elasticsearchProperties.getClusterNodes();
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        String[] nodesStringSplit = nodes.split(",");
        for (String nodeString : nodesStringSplit) {
            String[] nodeStringSplit = nodeString.split(":");
            if (nodeStringSplit.length == 2) {
                Map<String, Object> nodeMap = new HashMap<>();
                nodeMap.put("host", nodeStringSplit[0]);
                nodeMap.put("port", Integer.parseInt(nodeStringSplit[1]));
                list.add(nodeMap);
            }
        }
        return list;
    }

    /*
     * 初始化方法，在这个方法中将对象的属性赋值
     *
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        TransportClient preClient = new PreBuiltTransportClient(getSettings());
        ArrayList<Map<String, Object>> nodes = getNodes();
        for (Map<String, Object> node : nodes) {
            String host = (String) node.get("host");
            int port = (int) node.get("port");
            preClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        }
        client = preClient;
    }

    /*
     * 返取对象，将对象交由Spring的FactoryBen管理
     *
     * @see org.springframework.beans.factory.FactoryBean#getObject()
     */
    @Override
    public TransportClient getObject() throws Exception {
        return client;
    }

    /*
     * 返回getObject()方法返对象的反射对象（Class）
     *
     * @see org.springframework.beans.factory.FactoryBean#getObjectType()
     */
    @Override
    public Class<?> getObjectType() {
        return TransportClient.class;
    }

    /*
     * getObject()方法返对象是否是单例
     *
     * @see org.springframework.beans.factory.FactoryBean#isSingleton()
     */
    @Override
    public boolean isSingleton() {
        return true;
    }

}